package net.orange.framework.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

import net.orange.architecture.process.ProcessFactory;
import net.orange.architecture.web.BaseController;
import net.orange.framework.logic.IGroupProcess;
import net.orange.framework.model.Group;
import net.orange.framework.model.Resource;
import net.orange.framework.model.ResourceType;

/**
 * The resource managed bean using in JSF MVC modal. it is include the web page
 * content and control the action and page forward direction in the web site.
 */
@ManagedBean(name="resource")
@SessionScoped
public class ResourceController extends BaseController {

	/**
	 * The resource name want to search.
	 */
	private String sr_name;

	/**
	 * The resource URL want to search.
	 */
	private String sr_resource;

	/**
	 * The resource type want to search.
	 */
	private String s_type;

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.web.BaseController#getClazz()
	 */
	protected Class getClazz() {
		return Resource.class;
	}

	/**
	 * @return Returns the s_type.
	 */
	public String getS_type() {
		return s_type;
	}

	/**
	 * @return Returns the sr_name.
	 */
	public String getSr_name() {
		return sr_name;
	}

	/**
	 * @return Returns the sr_resource.
	 */
	public String getSr_resource() {
		return sr_resource;
	}

	/**
	 * @param s_type
	 *            The s_type to set.
	 */
	public void setS_type(String s_type) {
		this.s_type = s_type;
	}

	/**
	 * @param sr_name
	 *            The sr_name to set.
	 */
	public void setSr_name(String sr_name) {
		this.sr_name = sr_name;
	}

	/**
	 * @param sr_resource
	 *            The sr_resource to set.
	 */
	public void setSr_resource(String sr_resource) {
		this.sr_resource = sr_resource;
	}

	/**
	 * Return the select item (resource type) for selection component.
	 * 
	 * @return the collection of select item (resource type).
	 * @throws Exception
	 */
	public Collection getTypelist() throws Exception {
		List<SelectItem> list = new ArrayList<SelectItem>();

		for (ResourceType r : ResourceType.values())
			list.add(new SelectItem(r.getValue(), r.name()));

		return list;
	}

	/**
	 * @return Returns the group_pks.
	 */
	public String[] getGroup_pks() {
		Resource resource = (Resource) content;

		if (resource == null || resource.getGroups() == null
				|| resource.getGroups().size() <= 0)
			return null;

		Iterator groups = resource.getGroups().iterator();
		String[] pks = new String[resource.getGroups().size()];

		for (int i = 0; groups.hasNext(); ++i) {
			Group group = (Group) groups.next();
			pks[i] = group.getPk() + "";
		}

		return pks;
	}

	/**
	 * @param group_pks
	 *            The group_pks to set.
	 */
	public void setGroup_pks(String[] group_pks) throws Exception {
		Resource resource = (Resource) content;

		if (resource != null && group_pks != null && group_pks.length > 0) {
			IGroupProcess gprocess = (IGroupProcess) ProcessFactory
					.get(Group.class);

			resource.getGroups().clear();

			for (int i = 0; i < group_pks.length; i++) {
				String pk = group_pks[i];

				if (pk != null && pk.length() > 0) {
					Group group = (Group) gprocess.find(Long.parseLong(pk));
					if (group != null)
						resource.getGroups().add(group);
				}
			}
		}
	}
}
